package cn.actoncode.boot.module.ai.dal.redis.auth;

import cn.actoncode.boot.module.ai.controller.app.auth.vo.login.WebLoginRespVO;
import cn.actoncode.boot.module.ai.dal.redis.RedisKeyConstants;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class LoginMpQrCodeRedisDAO {

    @Resource
    private RedisTemplate<String, WebLoginRespVO> redisTemplate;

    public WebLoginRespVO get(String ticket) {
        String redisKey = formatKey(ticket);
        return redisTemplate.opsForValue().get(redisKey);
    }

    public void set(String ticket, WebLoginRespVO loginRespVO) {
        String redisKey = formatKey(ticket);
        redisTemplate.opsForValue().set(redisKey, loginRespVO);

        // 设置5分钟过期时间
        redisTemplate.expire(redisKey, 5, TimeUnit.MINUTES);
    }

    public void delete(String sk) {
        String redisKey = formatKey(sk);
        redisTemplate.delete(redisKey);
    }

    private static String formatKey(String ticket) {
        return String.format(RedisKeyConstants.LOGIN_MP_QR_CODE, ticket);
    }
}
